package io.renren.modules.business.service.impl;

import io.renren.modules.business.dao.BusAssessmentRecordDao;
import io.renren.modules.business.entity.BusAssessmentRecordEntity;
import io.renren.modules.business.service.BusAssessmentRecordService;
import io.renren.modules.business.util.PageLastUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;


@Service("busAssessmentRecordService")
public class BusAssessmentRecordServiceImpl extends ServiceImpl<BusAssessmentRecordDao, BusAssessmentRecordEntity> implements BusAssessmentRecordService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int aimsType;
        if (params.get("flag") != null) {
            aimsType = Integer.parseInt((String) params.get("flag"));
        } else {
            aimsType = 1;
        }
        String key = (String) params.get("userName");
        List<Map<String, Object>> list = new ArrayList<>();
        switch (aimsType) {
            case 1:
                list = studentAssess(key);
                break;
            case 2:
                list = tutorAssess(key);
                break;
            case 3:
                list = proAssess(key);
                break;
            case 4:
                list = teamAssessList(key);
                break;
            case 5:
                list = labAssessList(key);
                break;
        }
        return PageLastUtil.getPageUtils(params, list);
    }

    /**
     * 查询学生绩效考核记录
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> studentAssess(String userName) {
        if (userName != null && !"".equals(userName)) {
            return baseMapper.studentByKey(userName);
        } else {
            return baseMapper.studentList();
        }
    }

    /**
     * 查询导师绩效考核记录
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> tutorAssess(String userName) {
        if (userName != null && !"".equals(userName)) {
            return baseMapper.tutorByKey(userName);
        } else {
            return baseMapper.tutorList();
        }
    }

    /**
     * 查询项目绩效考核记录
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> proAssess(String userName) {
        if (userName != null && !"".equals(userName)) {
            return baseMapper.projectByKey(userName);
        } else {
            return baseMapper.projectList();
        }
    }

    /**
     * 查询团队绩效考核记录
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> teamAssessList(String userName) {
        teamAssess();
        if (userName != null && !"".equals(userName)) {
            return baseMapper.teamByKey(userName);
        } else {
            return baseMapper.teamList();
        }
    }

    /**
     * 查询实验室绩效考核记录
     *
     * @param userName
     * @return
     */
    public List<Map<String, Object>> labAssessList(String userName) {
        labAssess();
        if (userName != null && !"".equals(userName)) {
            return baseMapper.labByKey(userName);
        } else {
            return baseMapper.labList();
        }
    }

    /**
     * 团队绩效考核
     */
    public void teamAssess() {
        List<Long> teamIdList = baseMapper.allTeamId();
        for (Long id : teamIdList) {
            //获取团队所有成员绩效值的平均值
            double studentScore = baseMapper.studentScore(id) == null ? 0.0 : baseMapper.studentScore(id);
            //获取团队所有参与项目绩效值的平均值
            double proScore = baseMapper.projectScore(id) == null ? 0.0 : baseMapper.projectScore(id);
            double score = studentScore * 0.6 + proScore * 0.4;
            //判断团队绩效值是否存在，存在则修改，否，则新增
            if (baseMapper.isTeamScore(id).size() == 0) {
                baseMapper.teamRecord(id, score);
                baseMapper.teamScore(baseMapper.newRecordId(id), studentScore, proScore);
            } else {
                baseMapper.upTeamRecord(id, score);
                baseMapper.upTeamScore(baseMapper.newRecordId(id), studentScore, proScore);
            }
        }
    }

    /**
     * 对系统所有实验室进行绩效考核
     */
    public void labAssess() {
        List<Long> labIdList = baseMapper.allLabIdList();
        for (Long id : labIdList) {
            System.out.print("id：" + id);
            //查询实验室开发者绩效值的平均值
            double developerScore = baseMapper.developScore(id) == null ? 0.0 : baseMapper.developScore(id);
            //查询实验室导师绩效值的平均值
            double tutorScore = baseMapper.tutorScore(id) == null ? 0.0 : baseMapper.tutorScore(id);
            double score = developerScore * 0.5 + tutorScore * 0.5;
            //判断实验室绩效考核记录是否存在，存在，则修改，否，则将实验室绩效值添加到考核记录中
            if (baseMapper.isLabScore(id).size() == 0) {
                baseMapper.labRecord(id, score);
                baseMapper.labScore(baseMapper.newLabRecordId(id), score);
            }else {
                baseMapper.upLabRecord(id, score);
                baseMapper.upLabScore(baseMapper.newLabRecordId(id), score);
            }
        }
    }

}